package k1_jdbc.demo02.userOperation.controller;

import k1_jdbc.demo02.userOperation.entity.User;
import k1_jdbc.demo02.userOperation.service.UserService;
import k1_jdbc.tool.MD5Encryption;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class UserController {

    UserService us = new UserService();

    Scanner scanner = new Scanner(System.in);

    /**
     * 注册
     */
    public void register() throws Exception {
        System.out.println("=====欢迎来到注册页面=====");

        List<User> users = new ArrayList<>();

        String loginName;

        out:while(true){
            //从控制台获取登录名，判断登录名是否被使用过
            while(true){
                System.out.println("请输入登录名：");
                loginName = scanner.nextLine();
                User findUser = us.findUserByName(loginName);
                if (findUser!=null){
                    System.out.println("登录名已经存在");
                }else{
                    break;
                }
            }
            System.out.println("请输入昵称：");
            String userName = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = scanner.nextLine();

            //根据控制台输入的内容，封装一个User对象
            User user = new User(
                    UUID.randomUUID().toString().toUpperCase(),
                    userName,
                    loginName,
                    MD5Encryption.getEncryption(password),
                    0,
                    null
            );
            users.add(user);

            System.out.println("是否继续注册用户?[Y/N]");
            String isContinue = scanner.nextLine().toUpperCase();
            switch (isContinue){
                case "N":break out;
            }
        }

        //同时注册多个User用户
        boolean result = us.register(users);

        if(result){
            System.out.println("注册成功");
        }else{
            System.out.println("注册失败");
        }

    }

    /**
     * 实现登录的方法
     */
    public void login() throws Exception {

        System.out.println("=====欢迎来到登录页面=====");

        while(true){
            System.out.println("请输入登录名：");
            String loginName = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = MD5Encryption.getEncryption(scanner.nextLine());

            //根据登录名和密码查找数据，如果能找到就是登录成功，找不到就是登录失败
            User findUser = us.login(loginName,password);
            //如果这个对象不为空，表示登录成功，展示信息就可以了
            if(findUser!=null){
                System.out.println("欢迎你："+findUser.getUserName());
                System.out.println("当前是第"+findUser.getLoginCount()+"次登录");
                System.out.println("最后一次登录时间："+findUser.getLastLoginTime());
                break;
            }else{
                System.out.println("用户名或密码错误！");
            }
        }
    }



    public static void main(String[] args) throws Exception {
        UserController uc = new UserController();
//        uc.register();
        uc.login();
    }

}
